//
//  RegisterViewController.m
//  DriverClock
//
//  Created by admin on 2017/3/14.
//  Copyright © 2017年 童巍. All rights reserved.
//

#import "RegisterViewController.h"
#import "Tool.h"
#import "SVProgressHUD.h"
#import "Masonry.h"
#import "WTMNetworkTool.h"

#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

@interface RegisterViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *viewOne;
@property (weak, nonatomic) IBOutlet UIView *viewTwo;
@property (weak, nonatomic) IBOutlet UIView *viewThree;
@property (weak, nonatomic) IBOutlet UIView *viewFour;
@property (weak, nonatomic) IBOutlet UIImageView *viewOneImage;
@property (weak, nonatomic) IBOutlet UIImageView *viewTwoImage;
@property (weak, nonatomic) IBOutlet UIImageView *viewThreeImage;
@property (weak, nonatomic) IBOutlet UIImageView *vierFourImage;
@property (weak, nonatomic) IBOutlet UITextField *viewOneTF;
@property (weak, nonatomic) IBOutlet UITextField *viewTwoTF;
@property (weak, nonatomic) IBOutlet UITextField *viewThreeTF;
@property (weak, nonatomic) IBOutlet UITextField *viewFourTF;
@property (weak, nonatomic) IBOutlet UIButton *getCode;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;
@property (assign,nonatomic)NSInteger seconds;
@property (copy,nonatomic)NSDate *backgroundDate;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"免费注册";
    [self initUI];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnterForeground) name:@"EnterForeground" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnterBackground) name:@"EnterBackground" object:nil];
}

- (void)EnterForeground
{
    NSTimeInterval timeGone = [[NSDate date]timeIntervalSinceDate:_backgroundDate];
    self.seconds = self.seconds - (int)timeGone;
    
}
- (void)EnterBackground
{
    _backgroundDate = [NSDate date];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (IBAction)getVerifyCode:(UIButton *)sender {
    if (![Tool valiMobile:self.viewOneTF.text]) {
        [Tool errorWithMsg:@"请输入正确手机号"];
        return;
    }
    sender.enabled = NO;
    
    [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
    
        self.seconds -= 1;
        [sender setTitle:[NSString stringWithFormat:@"%ld",(long)self.seconds] forState:UIControlStateNormal];
        if (self.seconds <= 0) {
            sender.enabled = YES;
            [sender setTitle:@"重新获取" forState:UIControlStateNormal];
            [timer invalidate];
            self.seconds = 300;
        }
    }];
    
    
    [[WTMNetworkTool sharedNetworkTool] requestWithUrlstring:@"app/getVerificationCode" method:@"POST" parameters:@{@"params":@{@"mobileNumber":self.viewOneTF.text,@"purpose":@"2"}} success:^(id  _Nullable responseObject) {
        NSDictionary *loginMsg = [responseObject mutableCopy];
        [Tool errorWithMsg:loginMsg[@"resultMsg"]];
                if ([[loginMsg objectForKey:@"status"] integerValue] == 0) {
           
        }else{
            
        }
        
    } failure:^(NSError * _Nonnull error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"获取失败" message:@"请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [sender setTitle:@"重新获取" forState:UIControlStateNormal];
        sender.enabled = YES;
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.viewOneTF resignFirstResponder];
    [self.viewTwoTF resignFirstResponder];
    [self.viewThreeTF resignFirstResponder];
    [self.viewFourTF resignFirstResponder];
}

- (IBAction)makeSure:(UIButton *)sender {
    sender.enabled = NO;
    if (!self.viewOneTF.text||!(self.viewTwoTF.text)||!(self.viewThreeTF.text)||!(self.viewFourTF.text)) {
        [Tool errorWithMsg:@"资料不完整"];
        sender.enabled = YES;
    }
    if ([self.viewThreeTF.text isEqualToString:self.viewFourTF.text]) {

        [[WTMNetworkTool sharedNetworkTool] requestWithUrlstring:@"app/user/register" method:@"POST" parameters:@{@"params":@{@"mobileNumber":self.viewOneTF.text,@"verificationCode":self.viewTwoTF.text,@"DepartmentsId":@"18",@"password":[Tool md5:self.viewFourTF.text]}} success:^(id  _Nullable responseObject) {
//            NSLog(@"%@",responseObject);
            NSDictionary *loginMsg = [responseObject mutableCopy];
            if ([[loginMsg objectForKey:@"status"] integerValue] == 0) {
                [Tool errorWithMsg:[NSString stringWithFormat:@"%@",[loginMsg objectForKey:@"resultMsg"]]];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [Tool errorWithMsg:[NSString stringWithFormat:@"%@",[loginMsg objectForKey:@"resultMsg"]]];
            }
            sender.enabled = YES;
        } failure:^(NSError * _Nonnull error) {
            sender.enabled = YES;
        }];
    }else{
        [Tool errorWithMsg:@"两次密码不一致"];
        sender.enabled = YES;
    }
}


- (void)initUI
{
    [self.viewOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kWidthOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(kWidthOfPt(500));
    }];
    self.viewOne.layer.cornerRadius = kWidthOfPt(60);
    self.viewOne.clipsToBounds = YES;
    self.viewOne.layer.borderColor = [UIColor colorWithRed: 191/255.0 green:207/255.0 blue:229/255.0 alpha:1].CGColor;
    self.viewOne.layer.borderWidth = 1;
    
    [self.viewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kWidthOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(kWidthOfPt(500+170));
    }];
    self.viewTwo.layer.cornerRadius = kWidthOfPt(60);
    self.viewTwo.clipsToBounds = YES;
    self.viewTwo.layer.borderColor = [UIColor colorWithRed: 191/255.0 green:207/255.0 blue:229/255.0 alpha:1].CGColor;
    self.viewTwo.layer.borderWidth = 1;
    
    [self.viewThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kWidthOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(kWidthOfPt(500+170+170));
    }];
    self.viewThree.layer.cornerRadius = kWidthOfPt(60);
    self.viewThree.clipsToBounds = YES;
    self.viewThree.layer.borderColor = [UIColor colorWithRed: 191/255.0 green:207/255.0 blue:229/255.0 alpha:1].CGColor;
    self.viewThree.layer.borderWidth = 1;
    
    [self.viewFour mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kWidthOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(kWidthOfPt(500+170+170+170));
    }];
    self.viewFour.layer.cornerRadius = kWidthOfPt(60);
    self.viewFour.clipsToBounds = YES;
    self.viewFour.layer.borderColor = [UIColor colorWithRed: 191/255.0 green:207/255.0 blue:229/255.0 alpha:1].CGColor;
    self.viewFour.layer.borderWidth = 1;
    
    [self.viewOneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(50));
        make.centerY.mas_equalTo(self.viewOne.mas_centerY);
        make.width.mas_equalTo(kWidthOfPt(60));
        make.height.mas_equalTo(kWidthOfPt(60));
    }];
    
    [self.viewTwoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(50));
        make.centerY.mas_equalTo(self.viewTwo.mas_centerY);
        make.width.mas_equalTo(kWidthOfPt(60));
        make.height.mas_equalTo(kWidthOfPt(60));
    }];
    [self.viewThreeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(50));
        make.centerY.mas_equalTo(self.viewThree.mas_centerY);
        make.width.mas_equalTo(kWidthOfPt(60));
        make.height.mas_equalTo(kWidthOfPt(60));
    }];
    [self.vierFourImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(50));
        make.centerY.mas_equalTo(self.viewFour.mas_centerY);
        make.width.mas_equalTo(kWidthOfPt(60));
        make.height.mas_equalTo(kWidthOfPt(60));
    }];
    
    
    [self.viewOneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(140));
        make.right.mas_equalTo(kWidthOfPt(-80));
        make.centerY.mas_equalTo(self.viewOne.mas_centerY);
        make.height.mas_equalTo(self.viewOne.mas_height);
    }];
    self.viewOneTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号码" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17],     NSForegroundColorAttributeName:[UIColor colorWithRed: 191/255.0 green:207/255.0 blue:229/255.0 alpha:1]                                                }];
    
    [self.viewTwoTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(140));
        make.right.mas_equalTo(kWidthOfPt(-320));
        make.centerY.mas_equalTo(self.viewTwo.mas_centerY);
        make.height.mas_equalTo(self.viewTwo.mas_height);
    }];
    self.viewTwoTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17],     NSForegroundColorAttributeName:[UIColor colorWithRed: 191/255.0 green:207/255.0 blue:229/255.0 alpha:1]                                                }];
    
    [self.viewThreeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(140));
        make.right.mas_equalTo(kWidthOfPt(-80));
        make.centerY.mas_equalTo(self.viewThree.mas_centerY);
        make.height.mas_equalTo(self.viewThree.mas_height);
    }];
     self.viewThreeTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请设置密码" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17],     NSForegroundColorAttributeName:[UIColor colorWithRed: 191/255.0 green:207/255.0 blue:229/255.0 alpha:1]                                                }];
    
    [self.viewFourTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(140));
        make.right.mas_equalTo(kWidthOfPt(-80));
        make.centerY.mas_equalTo(self.viewFour.mas_centerY);
        make.height.mas_equalTo(self.viewFour.mas_height);
    }];
    self.viewFourTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请确认密码" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17],     NSForegroundColorAttributeName:[UIColor colorWithRed: 191/255.0 green:207/255.0 blue:229/255.0 alpha:1]                                                }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kWidthOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.viewFour.mas_bottom).offset(kWidthOfPt(160));
    }];
    self.sureButton.layer.cornerRadius = kWidthOfPt(60);
    self.sureButton.titleLabel.font = [UIFont systemFontOfSize:17];
    
    [self.getCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(kWidthOfPt(300));
    }];
    self.getCode.layer.cornerRadius = kWidthOfPt(60);
    self.getCode.titleLabel.font = [UIFont systemFontOfSize:17];
    
}

-(void)textFieldDidChange:(UITextField *)textfield
{
    if (textfield.text.length > 14 ) {
        textfield.text = [textfield.text substringWithRange:NSMakeRange(0, 14)];
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}


- (void)dealloc
{
//    [self.viewOneTF removeTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//    [self.viewTwoTF removeTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//    [self.viewThreeTF removeTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//    [self.viewFourTF removeTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}


@end
